{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lesson 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 题目1：文件和目录操作练习**\n",
    "\n",
    "1.在桌面上新建一个名为'lesson_5_test'的文件夹；\n",
    "\n",
    "2.现有如下文件filename_list = ['task1_20170701.txt', 'task1_20170601.txt', 'task1_20170501.txt','task2_20170701.txt', 'task2_20170601.txt', 'task2_20170501.txt']，在对应的task1、task2文件夹下新建列表中的文件；\n",
    "\n",
    "3.搜索'lesson_5_test'目录下七月份的文件，并在文件中写入‘hello'。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop')\n",
    "os.mkdir('lesson_5_test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\lesson_5_test')\n",
    "filename_list = ['task1_20170701.txt', 'task1_20170601.txt', 'task1_20170501.txt','task2_20170701.txt', 'task2_20170601.txt', 'task2_20170501.txt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "task1\n",
      "20170701.txt\n",
      "task1\n",
      "20170601.txt\n",
      "task1\n",
      "20170501.txt\n",
      "task2\n",
      "20170701.txt\n",
      "task2\n",
      "20170601.txt\n",
      "task2\n",
      "20170501.txt\n"
     ]
    }
   ],
   "source": [
    "for i in filename_list:\n",
    "    dir_name = i.split('_')[0]\n",
    "    file_name = i.split('_')[1]\n",
    "    print(dir_name)\n",
    "    print(file_name)\n",
    "    if os.path.exists(dir_name):\n",
    "        f = open(os.path.join(dir_name,i), 'w')\n",
    "        f.close() \n",
    "    else:\n",
    "        os.mkdir(dir_name)\n",
    "        f = open(os.path.join(dir_name,i), 'w')\n",
    "        f.close() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import glob\n",
    "ha = glob.glob('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\lesson_5_test\\\\*\\\\*07*.txt')\n",
    "for dir in ha:\n",
    "    with open(dir,'w') as txt:\n",
    "        txt.write('hello')\n",
    "        txt.close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
